import time

from common.base import Base, get_driver
from selenium.webdriver.common.by import By


class ShoppingPage(Base):
    def add_goods(self):
        """添加商品"""
        self.click((By.XPATH, "//img[@alt='continue']"))
        self.click((By.XPATH, "//img[@alt='随身风扇']"))
        self.click((By.XPATH, "//img[@src='themes/default/images/buybtn1.png']"))

    def click_modify_goods(self, number):
        """修改商品"""
        self.clear((By.XPATH, '//input[contains(@id,"goods_number")]'))
        self.send_keys((By.XPATH, '//input[contains(@id,"goods_number")]'), number)
        time.sleep(1)
        self.click((By.XPATH, "//input[@value='更新购物车']"))

    def del_goods(self):
        """删除商品"""
        self.click((By.LINK_TEXT, "删除"))
        alert = self.driver.switch_to.alert
        alert.accept()

    def look_up_goods(self):
        """查看购物车"""
        self.click((By.XPATH, "//a[@title='查看购物车']"))

    def click_clear_goods(self):
        """清空商品"""
        self.click((By.XPATH, "//input[@value='清空购物车']"))

    def click_checkout(self):
        """商品结算"""
        self.click((By.XPATH, "//img[@alt='checkout']"))


if __name__ == '__main__':
    # 创建driver
    driver = get_driver()
    driver.get("http://localhost:8080/ecshop/flow.php")
    driver.maximize_window()
    # 创建页面对象
    shopping = ShoppingPage(driver)
    shopping.add_goods()
    time.sleep(5)
    shopping.click_modify_goods(5)
    time.sleep(7)
    shopping.del_goods()
    time.sleep(5)
    shopping.look_up_goods()
    time.sleep(3)
    # shopping.click_clear_goods()
    # time.sleep(3)
    # shopping.click_checkout()
    # time.sleep(5)

    shopping.quit()